package com.learn.file;

import org.junit.Test;

import java.io.*;

public class StreamTranceform {
    @Test
    public void trance(){
        FileInputStream fis = null;
        InputStreamReader isr = null;
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        try {
            fis = new FileInputStream("hello.txt");
            isr = new InputStreamReader(fis, "UTF-8");

            fos = new FileOutputStream("helloEncodingByGBK.txt");
            osw = new OutputStreamWriter(fos,"GBK");

            char[] cbuf = new char[20];
            int len;
            while ((len = isr.read(cbuf)) != -1){
                osw.write(cbuf, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null) {
                    isr.close();
                }
                if (osw != null) {
                    osw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
